package com.zzld.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.entity.Res;
import com.zzld.modules.sys.mapper.ResMapper;
import com.zzld.modules.sys.service.IResService;

@Service("resService")
public class ResServiceImpl extends BaseServiceImpl<Res> implements IResService {

	@Autowired
	private ResMapper resMapper;
	
	public Res findById(String id){
		return resMapper.selectByPrimaryKey(id);
	}
	
	public int save(Res entity){
		return  resMapper.insertSelective(entity);
	}
	
	public int update(Res entity){
		return resMapper.updateByPrimaryKeySelective(entity);
	  
	}
	
	public int deleteById(String id){
		return resMapper.deleteByPrimaryKey(id);
	}
	
	
	public int selectbypid(String pid){
		
		return resMapper.selectbypid(pid);
		
	}
	
	/**
	 * 获取树形网格
	 * @return
	 */
	public List<TreeGrid> treeGrid(){
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		List<Res> listRes=resMapper.treeGrid();
		if(listRes !=null && listRes.size()>0){
			for (Res res : listRes) {
				TreeGrid treeGrid=new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setText(res.getName());
				treeGrids.add(treeGrid);
			}
		}
		return treeGrids;
	}
	
	/**
	 * 根据角色id获取属性网格
	 * @return
	 */
	public List<TreeGrid> treeGridbyroleid(String role_id){
		List<TreeGrid> roleGrids= new ArrayList<TreeGrid>();
		List<Res> listRes = resMapper.selectbyroleid(role_id);
		if(listRes != null && listRes.size()>0){
			for(Res res : listRes){
				TreeGrid treeGrid = new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setText(res.getName());
				roleGrids.add(treeGrid);
			}
		}
		return roleGrids;
	}
}
